//
//  EXT-UIBarButtonItem.swift
//  SSSina
//
//  Created by ss on 2017/6/29.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    //图片 名字 target action
    convenience init(imgName: String?,title:String?,target:Any?,action:Selector){
        //实例化一个按钮
        let button = UIButton()
        //监听事件
        button.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        
        //如果有图片就设置
        if let img = imgName  {
            //图片
            button.setImage(UIImage(named:img), for: UIControlState.normal)
            button.setImage(UIImage(named:"\(img)_highlighted"), for: UIControlState.highlighted)
        }
        if let tit = title {
            //如果有文字就设置
            button.setTitle(tit, for: UIControlState.normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            button.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
        }
        
        button.sizeToFit()
        
        //调用指定构造函数完成实例化
        self.init(customView:button)
    }
}
